package reflect.access;

import java.lang.reflect.Field;

/**
 * @version 1.0
 * @author:罗苏琴
 * @Date 2024/12/3 上午8:25
 */
public class AccessPersonFields {
    public static void main(String[] args) {
        try{
            //通过反射加载一个Person实例
            Class clz=Class.forName("reflect.entity.Person");
            Object person=clz.newInstance();
            Field name=clz.getDeclaredField("name");
            name.setAccessible(true);
            System.out.println("赋值前的name:"+name.get(person));
            name.set(person,"New Person");
            System.out.println("赋值后的name:"+name.get(person));
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (InstantiationException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        } catch (NoSuchFieldException e) {
            throw new RuntimeException(e);
        }
    }
}
